﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
//using SlimDX.DirectInput;
using DirectX.Capture;
using System.Net;
using System.Net.Sockets;


namespace ASCM_MulticopterApp
{
    public partial class FormMain : Form
    {
        internal Capture capture = null;
        private Filters filters = new Filters();
       // private BackgroundWorker bw = new BackgroundWorker();
        //Joystick Joystick;
        PhoneClient client;
        ArduinoCommunicator arduinoCom;
        bool controlerConnected = false;
        bool arduinoConnected = false;
        int minX; int maxX; int minY; int maxY; int mintT; int maxT;

        public FormMain()
        {
            InitializeComponent();
            client = new PhoneClient();
            arduinoCom = new ArduinoCommunicator();
            arduinoCom.onRefresh += new ArduinoCommunicator.RefreshInputRequest(On_requested_fresh_input);
            client.on_status_change += new PhoneClient.OnStatusChanged(StatusLabel);
          //  phoneClient.newData += new PhoneClient2.NewData(HeardIt);
            arduinoCom.status_changed += new ArduinoCommunicator.On_status_changed(On_arduino_error);
            Properties.Settings.Default.SettingChanging += SettingChanging;
           // toolStripStatusLabelWPhoneStatus.Image = Properties.Resources._true; toolStripStatusLabelArduinoStatus.Image = Properties.Resources._false;

          //  wpClient.onProblem += new WP8client.ProblemEvent(StatusLabel);
            Refresh();
        } 

     bool previous_status = false;
   

     private void StatusLabel(string msg,bool status)
     {
         if (status != previous_status)
         {
             if (status)
                 toolStripStatusLabelWPhoneStatus.Image = Properties.Resources._true;
             else 
                 toolStripStatusLabelWPhoneStatus.Image = Properties.Resources._false;
         }
         previous_status = status;
        // toolStripStatusLabelWPhoneStatus.Text = msg;         
     }

     void SettingChanging(object sender, System.Configuration.SettingChangingEventArgs e)
     {
       //  if(e.SettingName==)
         // Do something
     }
       



        bool previousArduinoStatus = false;
        private void On_arduino_error(string msg,bool status)
        {
            if (status != previousArduinoStatus)
            {
                if (status)
                    toolStripStatusLabelArduinoStatus.Image = Properties.Resources._true;
                else
                    toolStripStatusLabelArduinoStatus.Image = Properties.Resources._false;
            }
            previousArduinoStatus = status;
        }

        private void On_requested_fresh_input(ref int x, ref int y, ref int z)
        {
            arduinoCom.updatePositions(client.X, client.Y, client.Z, client.Throttle);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormSettings settings = new FormSettings(this);
            settings.Show();
        }

        
        internal void Refresh()
        {
            //try
            //{

            //    if (capture != null)
            //    {


            //        capture.PreviewWindow = null;

            //    }
               
            //    Filter videoDevice = null;
            //    Filter audioDevice = null;

            //    for(int x=0;x<filters.VideoInputDevices.Count;x++)
            //    {
            //        if(filters.VideoInputDevices[x].Name==Properties.Settings.Default.VideoDevice)
            //        {videoDevice = filters.VideoInputDevices[x];break;}
            //    }

            //    capture = new Capture(videoDevice, audioDevice);
            //   // capture.FrameRate = 29.997;                 // NTSC
            //    capture.FrameSize = Properties.Settings.Default.VideoResolution;   // 720x480
            //    capture.PreviewWindow = panelLiveView;

            //    arduinoCom.Disconnect();
            //    arduinoCom.Connect(Properties.Settings.Default.ArduinoComPortName, 115200);
            //    client.connect(Properties.Settings.Default.WPhoneIpAddress, 3011);
               
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("Unable to enable/disable preview. \n\n" + ex.Message + "\n\n"/* + ex.ToString()*/);
            //}
        }

        public double ScaleRange(double Value,double FromMinValue, double FromMaxValue,double ToMinValue, double ToMaxValue)
        {
            try
            {
                return (Value - FromMinValue) *
                    (ToMaxValue - ToMinValue) /
                    (FromMaxValue - FromMinValue) + ToMinValue;
            }
            catch
            {
                return double.NaN;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {

                if (capture != null)
                {


                    capture.PreviewWindow = null;

                }

                Filter videoDevice = null;
                Filter audioDevice = null;

                for (int x = 0; x < filters.VideoInputDevices.Count; x++)
                {
                    if (filters.VideoInputDevices[x].Name == Properties.Settings.Default.VideoDevice)
                    { videoDevice = filters.VideoInputDevices[x]; break; }
                }

                capture = new Capture(videoDevice, audioDevice);
                // capture.FrameRate = 29.997;                 // NTSC
                capture.FrameSize = Properties.Settings.Default.VideoResolution;   // 720x480
                capture.PreviewWindow = panelLiveView;

               
                arduinoCom.Connect(Properties.Settings.Default.ArduinoComPortName, 115200);
                client.connect(Properties.Settings.Default.WPhoneIpAddress, 3011);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to enable/disable preview. \n\n" + ex.Message + "\n\n"/* + ex.ToString()*/);
            }
        }
      

      

    }
}
